# -*- coding: utf-8 -*-
from __future__ import print_function
from collections import defaultdict
from Cython.Distutils import build_ext
from distutils.core import setup
from distutils.extension import Extension
# from Cython.Build import cythonize    # MacOS NG
from setuptools import setup, find_packages, Extension

import subprocess
import numpy
import sys
import platform
import os
import time
import pybind11

import shutil
from ctypes.util import find_library
setup_requires = []
install_requires = [
    'filelock',
    'mock',
    'nose',
    # RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility
    # https://github.com/scikit-image/scikit-image/issues/3655
    # 'numpy>=1.15.1,!=1.50.0',
    # numpy.ufunc size changed, may indicate binary incompatibility.
    'numpy>=1.16.1,!=1.16.2',
    'Cython>=0.26.0',
]


# def pkgconfig(flag):
#     # Equivalent in Python 2.7 (but not 2.6):
#     # subprocess.check_output(['pkg-config', flag] + pcl_libs).split()
#     p = subprocess.Popen(['pkg-config', flag] +
#                          pcl_libs, stdout=subprocess.PIPE)
#     stdout, _ = p.communicate()
#     # Assume no evil spaces in filenames; unsure how pkg-config would
#     # handle those, anyway.
#     # decode() is required in Python 3. TODO how do know the encoding?
#     return stdout.decode().split()



if platform.system() == "Windows":
    pass

else:


    ext_args = defaultdict(list)
    ext_args['include_dirs'].append(numpy.get_include())

    if sys.platform == 'darwin':
        # not use gcc?
        # ext_args['extra_compile_args'].append("-stdlib=libstdc++")
        # clang(min : 10.7?/10.9?)
        # minimum deployment target of OS X 10.9
        ext_args['extra_compile_args'].append("-stdlib=libc++")
        ext_args['extra_compile_args'].append("-mmacosx-version-min=10.9")
        ext_args['extra_link_args'].append("-stdlib=libc++")
        ext_args['extra_link_args'].append("-mmacosx-version-min=10.9")
        # vtk error : not set override function error.
        ext_args['extra_compile_args'].append("-std=c++11")
        # mac os using openmp
        # https://iscinumpy.gitlab.io/post/omp-on-high-sierra/
        # before setting.
        # $ brew install libomp
        # ext_args['extra_compile_args'].append('-fopenmp -Xpreprocessor')
        # ext_args['extra_link_args'].append('-fopenmp -Xpreprocessor -lomp')
        pass
    else:
        ext_args['extra_compile_args'].append("-std=c++14")
        ext_args['library_dirs'].append("/usr/lib/x86_64-linux-gnu/")
        # gcc? use standard library
        # ext_args['extra_compile_args'].append("-stdlib=libstdc++")
        # ext_args['extra_link_args'].append("-stdlib=libstdc++")
        # clang use standard library
        # ext_args['extra_compile_args'].append("-stdlib=libc++")
        # ext_args['extra_link_args'].append("-stdlib=libc++")
        # using openmp
        # ext_args['extra_compile_args'].append('-fopenmp')
        # ext_args['extra_link_args'].append('-fopenmp')
        pass

    ext_args['include_dirs'].append(pybind11.get_include())
    ext_args['include_dirs'].append("math_core/include")
    ext_args['include_dirs'].append("/usr/local/cuda/include")
    ext_args['include_dirs'].append("/usr/include/opencv4")
    ext_args['include_dirs'].append("/usr/include/opencv4/opencv2")
    ext_args['libraries'] = ["opencv_features2d","opencv_highgui","opencv_core"]
    ext_args['library_dirs'] = ["/usr/include/opencv4"]
    # ext_args['define_macros'].append(
    #     ("EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET", "1"))

    module = [Extension("mypymath", ["math_core/src/mypymath.cpp", "math_core/src/mymath.cpp"], language="c++", **ext_args),
                # Extension("pcl.pcl_grabber", ["pcl/pcl_grabber.pyx", "pcl/grabber_callback.cpp"], language="c++", **ext_args),
                # debug
                # gdb_debug=True,
                ]
    # listDlls = []
    data_files = None

# setup(ext_modules=[functions_module])
setup(name='my-pymath',
      description='Python bindings for the math_core. using Cython.',
      url='http://github.com/strawlab/python-pcl',
      version='0.1.0MyPyMath',
      author='Yalin Huang',
      author_email='yalin.huang@cripac.ia.ac.cn',
      maintainer='Yalin Huang',
      maintainer_email='yalin.huang@cripac.ia.ac.cn',
      license='BSD',
      packages=[
          "mypymath",
          # "pcl.pcl_visualization",
      ],
      zip_safe=False,
      classifiers=[
          'Programming Language :: Python :: 3.5',
          'Programming Language :: Python :: 3.6',
          'Programming Language :: Python :: 3.7',
      ],
      ext_modules=module,
      cmdclass={'build_ext': build_ext},
      data_files=data_files
      )
